package com.roads.workflow.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.roads.common.core.domain.entity.SysUser;
import com.roads.common.utils.StringUtils;
import com.roads.system.mapper.SysUserMapper;
import com.roads.workflow.domain.WorkflowMeterageNode;
import com.roads.workflow.mapper.WorkflowMeterageNodeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.roads.workflow.mapper.WorkflowMeterageNodeUserMapper;
import com.roads.workflow.domain.WorkflowMeterageNodeUser;
import com.roads.workflow.service.IWorkflowMeterageNodeUserService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 流程管理-计量流程节点用户Service业务层处理
 * 
 * @author nhp
 * @date 2022-05-30
 */
@Service
public class WorkflowMeterageNodeUserServiceImpl implements IWorkflowMeterageNodeUserService {

    @Autowired
    private WorkflowMeterageNodeUserMapper mapper;

    @Autowired
    private WorkflowMeterageNodeMapper workflowMeterageNodeMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询列表
     *
     * @param model 流程管理-计量流程节点
     * @return 流程管理-计量流程节点
     */
    @Override
    public List<WorkflowMeterageNodeUser> selectModelList(WorkflowMeterageNodeUser model) {
        return mapper.selectModelList(model);
    }

    /**
     * 保存流程节点用户
     *
     * @param workflowNodeUuid 流程几点uuid
     * @param users 用户集合
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveWorkflowNodeUser(String workflowNodeUuid, String users) {
        // 删除用户
        mapper.deleteByWorkflowNodeUuid(workflowNodeUuid);
        if (StringUtils.isNotEmpty(users)) {
            WorkflowMeterageNode node = workflowMeterageNodeMapper.getModelById(workflowNodeUuid);
            // 转集合，批量插入
            List<WorkflowMeterageNodeUser> list = new ArrayList<>();
            String[] userArray = users.split(",");
            if (userArray.length >0) {
                for (int i = 0; i < userArray.length; i++) {
                    String user = userArray[i];
                    SysUser sysUser = sysUserMapper.selectUserByUserName(user);
                    WorkflowMeterageNodeUser nodeUser = new WorkflowMeterageNodeUser();
                    nodeUser.setWorkflowNodeUuid(workflowNodeUuid);
                    nodeUser.setUser(user);
                    nodeUser.setName(sysUser.getNickName());
                    nodeUser.setWorkflowUuid(node.getWorkflowUuid());
                    list.add(nodeUser);
                }
                mapper.batchInsert(list);
            }
        }

        return 1;
    }

    @Override
    public int deleteByWorkflowNodeUuid(String workflowNodeUuid) {
        return mapper.deleteByWorkflowNodeUuid(workflowNodeUuid);
    }

    @Override
    public int deleteByWorkflowUuid(String workflowUuid) {
        return mapper.deleteByWorkflowUuid(workflowUuid);
    }

    @Override
    public int batchInsert(List<WorkflowMeterageNodeUser> list) {
        return mapper.batchInsert(list);
    }
}
